package com.bootdo.contract.controller;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import com.bootdo.system.domain.DeptDO;
import com.bootdo.system.domain.UserDO;
import com.bootdo.system.service.DeptService;
import com.bootdo.system.service.UserService;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.ui.Model;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.bootdo.contract.domain.SiteDO;
import com.bootdo.contract.service.SiteService;
import com.bootdo.common.utils.PageUtils;
import com.bootdo.common.utils.Query;
import com.bootdo.common.utils.R;

import javax.servlet.http.HttpServletRequest;

/**
 * 
 * 
 * @author yang
 * @email 2795210596@qq.com
 * @date 2019-04-27 16:12:50
 */
 
@Controller
@RequestMapping("/contract/site")
public class SiteController {
	@Autowired
	private SiteService siteService;
	@Autowired
	private DeptService deptService;
	@Autowired
	private UserService userService;
	
	@GetMapping()
	@RequiresPermissions("contract:site:site")
	String Site(){
	    return "contract/site/site";
	}
	
	@ResponseBody
	@GetMapping("/list")
	@RequiresPermissions("contract:site:site")
	public PageUtils list(@RequestParam Map<String, Object> params){
		//查询列表数据
        Query query = new Query(params);
		List<SiteDO> siteList = siteService.list(query);
		int total = siteService.count(query);
		PageUtils pageUtils = new PageUtils(siteList, total);
		return pageUtils;
	}
	
	@GetMapping("/add")
	@RequiresPermissions("contract:site:add")
	String add(){
	    return "contract/site/add";
	}

	@GetMapping("/edit/{id}")
	@RequiresPermissions("contract:site:edit")
	String edit(@PathVariable("id") Long id,Model model){
		SiteDO site = siteService.get(id);
		model.addAttribute("site", site);
	    return "contract/site/edit";
	}
	
	/**
	 * 保存
	 */
	@ResponseBody
	@PostMapping("/save")
	@RequiresPermissions("contract:site:add")
	public R save( SiteDO site){
		if(siteService.save(site)>0){
			return R.ok();
		}
		return R.error();
	}
	/**
	 * 修改
	 */
	@ResponseBody
	@RequestMapping("/update")
	@RequiresPermissions("contract:site:edit")
	public R update( SiteDO site){
		siteService.update(site);
		return R.ok();
	}
	
	/**
	 * 删除
	 */
	@PostMapping( "/remove")
	@ResponseBody
	@RequiresPermissions("contract:site:remove")
	public R remove( Long id){
		if(siteService.remove(id)>0){
		return R.ok();
		}
		return R.error();
	}
	
	/**
	 * 删除
	 */
	@PostMapping( "/batchRemove")
	@ResponseBody
	@RequiresPermissions("contract:site:batchRemove")
	public R remove(@RequestParam("ids[]") Long[] ids){
		siteService.batchRemove(ids);
		return R.ok();
	}

	@PostMapping("/getUserSite")
	@ResponseBody
	public R getUserSite(String gather) {
		Map<String, Object> params = new HashMap<>();

		String [] gathers = gather.split(",");
		params.put("userid",gathers[0]);
		if(!gathers[1].equals("1")){
			/*根据T分割后拼装*/
			String [] startTimes = gathers[1].split("T");
			String startTime = startTimes[0]+" "+startTimes[1]+":00";
			params.put("startTime",startTime);
		}
		if(!gathers[2].equals("1")){
			String [] endTimes = gathers[2].split("T");
			String endTime = endTimes[0]+" "+endTimes[1]+":00";
			params.put("endTime",endTime);
		}
		List<SiteDO> siteDOs = siteService.list(params);
		String lujing = "";
		String qi = "";
		String zhong = "";
		if(siteDOs.size() > 0){
			for (int i = 0; i < siteDOs.size(); i++) {
				lujing +=  "["+siteDOs.get(i).getLongitude()+","+siteDOs.get(i).getLatitude()+"],";
			}
			lujing = lujing.substring(0, lujing.length() - 1);
			qi = siteDOs.get(0).getLongitude();
			zhong = siteDOs.get(0).getLatitude();
		}
		/*返回今日第一条为起始*/

		return R.ok().put("lujing",lujing).put("qi",qi).put("zhong",zhong);
	}

	@PostMapping("/getDeptjw")
	@ResponseBody
	public R getDeptjw(String deptId){
		return R.ok().put("dept", deptService.get(Long.valueOf(deptId)));
	}

	@PostMapping("/getRealUserSite")
	@ResponseBody
	public R getRealUserSite(String userId){
		SimpleDateFormat  sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm");
		System.out.println();
		/*根据userid传递user*/
		UserDO userDO = userService.get(Long.valueOf(userId));
		SiteDO siteDO = siteService.getRealUserSite(Long.valueOf(userId));
		String date = "";
		if(siteDO != null){
			date = sdf.format(siteDO.getGmtCreate());
		}
		return R.ok().put("dept", siteDO).put("user",userDO.getName()).put("date",date);
	}


}
